package com.ccx.coachsite.ui.share;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.TextView;
import android.widget.Toast;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.CustomInfo;
import com.ccx.coachsite.ui.CommonAppView;
import com.ccx.coachsite.ui.share.MyCustomerView.OnBackListener;
import com.ccx.coachsite.util.ContactInfo;
import com.ccx.coachsite.util.ContactsUtil;
import com.ccx.coachsite.util.OpenShareTool;

/**
 * 通讯录View Tab
 */
public class PhoneBookView extends CommonAppView implements OnClickListener{
	
	private Context ctx;
	private BaseAdapter adapter;  
    private ListView personList;  
    private Button shareButtonCustomSend;
	private OpenSendListener mOpenSend;
    private AsyncQueryHandler asyncQuery;  
    private static final String NAME = "name", NUMBER = "number",  SORT_KEY = "sort_key"; 
    private View btnBack;
    
    private HashMap<String,String> selectIDsMap = new HashMap<String,String>();

	public PhoneBookView(Context paramContext) {
		super(paramContext);
		
		ctx = paramContext;
		
		addView(R.layout.open_share_phonebook_list);
		
		shareButtonCustomSend = (Button) findViewById(R.id.share_button_custom_send); 
		shareButtonCustomSend.setOnClickListener(this);
        personList = (ListView) findViewById(R.id.listView);  
        asyncQuery = new MyAsyncQueryHandler(paramContext.getContentResolver()); 
        btnBack = findViewById(R.id.share_button_book_back);
		btnBack.setOnClickListener(new OnBackListener());
		
        loadData();
	}
	
	class OnBackListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Activity act = (Activity)ctx;
			if(act != null)
				act.onBackPressed();
		}
	}
	
	public void setOpenSendListener(OpenSendListener openSend){
		mOpenSend = openSend;
	}

	/**
	 * loadData(加载数据)
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void loadData() {
		
        Uri uri = Uri.parse("content://com.android.contacts/data/phones"); // 联系人的Uri  
        String[] projection = { "_id", "display_name", "data1", "sort_key" }; // 查询的列   
        asyncQuery.startQuery(0, null, uri, projection, null, null,  
                "sort_key COLLATE LOCALIZED asc"); // 按照sort_key升序查询
    } 
	
	/** 
     * 数据库异步查询类AsyncQueryHandler 
     *  
     * @author administrator 
     *  
     */  
    private class MyAsyncQueryHandler extends AsyncQueryHandler {  
  
        public MyAsyncQueryHandler(ContentResolver cr) {  
            super(cr);  
        }  
  
        /** 
         * 查询结束的回调函数 
         */  
        @Override  
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {  
            if (cursor != null && cursor.getCount() > 0) {  
                List<ContentValues> list = new ArrayList<ContentValues>();  
                cursor.moveToFirst();  
                for (int i = 0; i < cursor.getCount(); i++) {  
                    ContentValues cv = new ContentValues();  
                    cursor.moveToPosition(i);  
                    String name = cursor.getString(1);  
                    String number = cursor.getString(2);  
                    String sortKey = cursor.getString(3);  
  
                    if ( ContactsUtil.checkContact(number) ) {
                        cv.put(NAME, name);  
                        cv.put(NUMBER, number);  
                        cv.put(SORT_KEY, sortKey);  
                        
                        if ( !list.contains(cv) ){
                        
                        	list.add(cv); 
                        }
                    }  
                }  
                if (list.size() > 0) {  
                    setAdapter(list);  
                }  
            }  
        }  
  
    }  
  
    private void setAdapter(List<ContentValues> list) {  
        adapter = new PhoneListAdapter(ctx, list);  
        personList.setAdapter(adapter);  
  
    }  
  
    private static class ViewHolder {  
    	public TextView alpha;
		public TextView name;
		public TextView number;
		public ImageView checkImg;
		
		public ViewHolder(View view) {
			alpha = (TextView) view.findViewById(R.id.alpha);
			name = (TextView) view.findViewById(R.id.name);
			number = (TextView) view.findViewById(R.id.number);
			checkImg = (ImageView) view.findViewById(R.id.checkImg);
		}
    }  
  
    /** 
     * 其他项目使用时，只需要传进来一个有序的list即可 
     */  
    private class PhoneListAdapter extends BaseAdapter implements SectionIndexer{  
        private LayoutInflater inflater;  
        private List<ContentValues> list;  
        private HashMap<String, Integer> alphaIndexer;//保存每个索引在list中的位置【#-0，A-4，B-10】   
        private String[] sections;//每个分组的索引表【A,B,C,F...】   
  
        public PhoneListAdapter(Context context, List<ContentValues> list) {  
            this.inflater = LayoutInflater.from(context);  
            this.list = list; // 该list是已经排序过的集合，有些项目中的数据必须要自己进行排序。   
            this.alphaIndexer = new HashMap<String, Integer>();  
  
            for (int i =0; i <list.size(); i++) {  
                String name = getAlpha(list.get(i).getAsString(SORT_KEY));  
                if(!alphaIndexer.containsKey(name)){//只记录在list中首次出现的位置   
                    alphaIndexer.put(name, i);  
                }  
            }  
            Set<String> sectionLetters = alphaIndexer.keySet();  
            ArrayList<String> sectionList = new ArrayList<String>(  
                    sectionLetters);  
            Collections.sort(sectionList);  
            sections = new String[sectionList.size()];  
            sectionList.toArray(sections);  
        }  
  
        @Override  
        public int getCount() {  
            return list.size();  
        }  
  
        @Override  
        public Object getItem(int position) {  
            return list.get(position);  
        }  
  
        @Override  
        public long getItemId(int position) {  
            return position;  
        }  
  
        @Override  
        public View getView(int position, View convertView, ViewGroup parent) {  
            ViewHolder holder;  
  
            if (convertView == null) {  
                convertView = inflater.inflate(R.layout.open_share_phonebook_list_item, null);  
                holder = new ViewHolder(convertView);  
                convertView.setTag(holder);  
            } else {  
                holder = (ViewHolder) convertView.getTag();  
            }  
            ContentValues cv = list.get(position);  
            String name = cv.getAsString(NAME);  
            String number = cv.getAsString(NUMBER);  
            holder.name.setText(name);  
            holder.number.setText(number);  
  
            // 当前联系人的sortKey   
            String currentStr = getAlpha(list.get(position).getAsString(  
                    SORT_KEY));  
            // 上一个联系人的sortKey   
            String previewStr = (position - 1) >= 0 ? getAlpha(list.get(  
                    position - 1).getAsString(SORT_KEY)) : " ";  
            /** 
             * 判断显示#、A-Z的TextView隐藏与可见 
             */  
            if (!previewStr.equals(currentStr)) { // 当前联系人的sortKey！=上一个联系人的sortKey，说明当前联系人是新组。   
                holder.alpha.setVisibility(View.VISIBLE);  
                holder.alpha.setText(currentStr);  
            } else {  
                holder.alpha.setVisibility(View.GONE);  
            }  
            
            holder.checkImg.setSelected( selectIDsMap.containsKey(holder.number.getText().toString()) );
			
			convertView.setOnClickListener(new OnClickListener() {
	            @Override
	            public void onClick(View v) {
	    
	            	ViewHolder clickCache = (ViewHolder) v.getTag();
	            	if ( !selectIDsMap.containsKey(clickCache.number.getText().toString()) ){
	            		selectIDsMap.put(clickCache.number.getText().toString(), clickCache.number.getText().toString());
	            	}else{
	            		selectIDsMap.remove(clickCache.number.getText().toString());
	            	}
	            	
	            	notifyDataSetChanged();
	            }
	        });
			
            return convertView;  
        }  
  
        /**
         * 此方法根据联系人的首字母返回在list中的位置 
         */  
        @Override  
        public int getPositionForSection(int section) {  
            String later = sections[section];  
            return alphaIndexer.get(later);  
        }
        
        @Override  
        public int getSectionForPosition(int position) {  
            String key = getAlpha(list.get(position).getAsString(SORT_KEY));  
            for (int i = 0; i < sections.length; i++) {  
                if (sections[i].equals(key)) {  
                    return i;  
                }  
            }  
            return 0;  
        } 
        
        @Override  
        public Object[] getSections() {  
            return sections;  
        }
     }  
    
    /** 
     * 提取英文的首字母，非英文字母用#代替 
     *  
     * @param str 
     * @return 
     */  
    private String getAlpha(String str) {  
        if (str == null) {  
            return "#";  
        }  
  
        if (str.trim().length() == 0) {  
            return "#";  
        }  
  
        char c = str.trim().substring(0, 1).charAt(0);  
        // 正则表达式，判断首字母是否是英文字母   
        Pattern pattern = Pattern.compile("^[A-Za-z]+$");  
        if (pattern.matcher(c + "").matches()) {  
            return (c + "").toUpperCase(); // 大写输出   
        } else {  
            return "#";  
        }  
    }
    
    @Override
	public void onClick(View v) {
		if (v == shareButtonCustomSend) {
			
			final String toShareUserIds = getShareUserIds();
			
			Log.d("toShareUserIds=", toShareUserIds);
			
			if ( "".equals(toShareUserIds) ) {
				
				Toast toast = Toast.makeText(ctx, "请选择要"+OpenShareTool.getShareDesc(ctx)+"的人", Toast.LENGTH_SHORT);
				//toast.setGravity(Gravity.CENTER, 0, 0);// 设置居中
				toast.show();
				return ;
			}
			
			if (mOpenSend!=null)
				mOpenSend.sendSMS(3, toShareUserIds);
		}	
	}
	
	public String getShareUserIds(){
		
		String resultUserIds = "";
		
		if ( selectIDsMap.size()>0 ){
	        Set<String> key = selectIDsMap.keySet();
	        for (Iterator it = key.iterator(); it.hasNext();) {
	            String s = (String) it.next();
	            resultUserIds = resultUserIds+s+",";
	        }
	        
	        if ( resultUserIds.endsWith(",") ){
	        	resultUserIds = resultUserIds.substring(0, resultUserIds.length()-1);
	        }
		}
		
		return resultUserIds;
	}

}
